package com.control.third;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.GoodsInfoDAO;
import com.dao.GoodsTypeDAO;
import com.dao.GoodsTypeDetailInfoDAO;
import com.entity.GoodsInfo;
import com.entity.GoodsTypeDetail;
import com.util.PublicToolCheckParam;
import com.util.PublicToolShowGoodsInfolist;
import com.util.Validate;

public class ManageGoodsInfo extends HttpServlet {

    /**
     *
     */
    private static final long serialVersionUID = 7321956339019190364L;
    static GoodsInfoDAO gdao = new GoodsInfoDAO();

    public static void deleteGoodsInfo(HttpServletRequest request,
                                       HttpServletResponse response) throws ServletException, IOException {

        String goodsId = Validate.replaceHtml(request.getParameter("goodsId"));
        String thirdId = Validate.replaceHtml(request.getParameter("thirdId"));
        if (PublicToolCheckParam.checkNullAndEmpty(goodsId)) {
            gdao.deleteGoodsInfo(Integer.parseInt(goodsId));
            String isSearching = Validate.replaceHtml(request
                    .getParameter("isSearching"));
            String cp = Validate.replaceHtml(request.getParameter("cp"));
            if (PublicToolCheckParam.checkNullAndEmpty(isSearching)) {
                String key = Validate.replaceHtml(request.getParameter("key"));
                PublicToolShowGoodsInfolist.searchGoodsInfoforthirdId(request,
                        response, cp, key, thirdId);
                request.setAttribute("isSearching", "yes");
            } else {
                PublicToolShowGoodsInfolist.showGoodsInfoListforthirdId(
                        request, response, cp, thirdId);
            }
        }
        request.getRequestDispatcher("../third/manageGoodsInfo.jsp").forward(
                request, response);

    }

    public static void deleteMoreGoodsInfo(HttpServletRequest request,
                                           HttpServletResponse response) throws ServletException, IOException {
        String thirdId = Validate.replaceHtml(request.getParameter("thirdId"));

        String cp = Validate.replaceHtml(request.getParameter("cp"));
        String[] ckb = request.getParameterValues("ckb");
        if (ckb != null) {
            for (int i = 0; i < ckb.length; i++) {
                gdao.deleteGoodsInfo(Integer.parseInt(ckb[i]));
            }
        }
        PublicToolShowGoodsInfolist.showGoodsInfoListforthirdId(request,
                response, cp, thirdId);
        request.getRequestDispatcher("../third/manageGoodsInfo.jsp").forward(
                request, response);

    }

    public static void insertGoodsInfo(HttpServletRequest request,
                                       HttpServletResponse response) throws ServletException, IOException {
        String thirdId = Validate.replaceHtml(request.getParameter("thirdId"));

        String str_goodsName = Validate.replaceHtml(request
                .getParameter("goodsName"));
        String typeDetailInfo = Validate.replaceHtml(request
                .getParameter("typeDetailInfo"));
        String str_price = Validate.replaceHtml(request.getParameter("price"));

        String str_photo = Validate.replaceHtml(request.getParameter("photo"));
        String str_remark = Validate
                .replaceHtml(request.getParameter("remark"));
        
        // String deadline =
        // Validate.replaceHtml(request.getParameter("deadline");
         int status = 1;
        String photo = null;
        if (PublicToolCheckParam.checkNullAndEmpty(str_goodsName)
                && PublicToolCheckParam.checkNullAndEmpty(typeDetailInfo)
                && PublicToolCheckParam.checkNullAndEmpty(str_price)
             ) {
            
            if (PublicToolCheckParam.checkNullAndEmpty(str_photo)) {
                String[] photoArray = str_photo.split("\\\\");
                photo = photoArray[photoArray.length - 1];
                photo = photo.replaceAll("'", "��");

            }

            str_goodsName = str_goodsName.replaceAll("'", "��");
            gdao.addGoodsInfo(typeDetailInfo, str_goodsName,
                    Double.parseDouble(str_price), 1, 1,
                    1, status, photo, str_remark,
                    1, thirdId, "1");

            String cp = Validate.replaceHtml(request.getParameter("cp"));
            PublicToolShowGoodsInfolist.showGoodsInfoListforthirdId(request,
                    response, cp, thirdId);
            request.getRequestDispatcher("../third/manageGoodsInfo.jsp")
                    .forward(request, response);
        }
    }

    public static void getTypeDetailInfo(HttpServletRequest request,
                                         HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        String typeId = Validate.replaceHtml(request
                .getParameter("goodsTypeId"));
        GoodsTypeDetailInfoDAO gtddao = new GoodsTypeDetailInfoDAO();
        ArrayList<Object> typeDetailInfoList = gtddao
                .GoodsTypeDetailAllInfoDAO(Integer.parseInt(typeId));
        if (typeDetailInfoList != null) {
            for (int i = 0; i < typeDetailInfoList.size(); i++) {
                GoodsTypeDetail typeDetailInfo = (GoodsTypeDetail) typeDetailInfoList
                        .get(i);
                out.println(typeDetailInfo.getTypeDetailInfo() + ","
                        + typeDetailInfo.getId() + "|");
            }
        }
        out.flush();
        out.close();
    }

    public static void searchGoodsInfo(HttpServletRequest request,
                                       HttpServletResponse response) throws ServletException, IOException {
        String thirdId = Validate.replaceHtml(request.getParameter("thirdId"));

        String cp = Validate.replaceHtml(request.getParameter("cp"));
        String key = Validate.replaceHtml(request.getParameter("key"));
        key = new String(key.getBytes("iso-8859-1"), "utf-8");
        PublicToolShowGoodsInfolist.searchGoodsInfoforthirdId(request,
                response, cp, key, thirdId);
        request.setAttribute("isSearching", "yes");
        request.setAttribute("key", key);
        request.getRequestDispatcher("../third/manageGoodsInfo.jsp").forward(
                request, response);
    }
    
    public static void showAddGoodsInfoPage(HttpServletRequest request,
                                            HttpServletResponse response) throws ServletException, IOException {
        String thirdId = Validate.replaceHtml(request.getParameter("thirdId"));
        GoodsTypeDAO gtdao = new GoodsTypeDAO();
        ArrayList<Object> typeList = gtdao.getAllType();
        GoodsTypeDetailInfoDAO gtddao = new GoodsTypeDetailInfoDAO();
        ArrayList<Object> typeDetailInfoList = gtddao.GoodsTypeDetailAllInfoDAO(6);
        request.setAttribute("typeList", typeList);
        request.setAttribute("typeDetailInfoList", typeDetailInfoList);
        request.setAttribute("thirdId", thirdId);

        request.getRequestDispatcher("../third/addGoodsInfo.jsp").forward(
                request, response);

    }
    /**
     * 坐诊信息管理列表
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public static void showManageGoodsInfoPage(HttpServletRequest request,
                                               HttpServletResponse response) throws ServletException, IOException {
        String cp = Validate.replaceHtml(request.getParameter("cp"));

        String thirdId = Validate.replaceHtml(request.getParameter("thirdId"));
        PublicToolShowGoodsInfolist.showGoodsInfoListforthirdId(request,response, cp, thirdId);
        request.getRequestDispatcher("../third/manageGoodsInfo.jsp").forward(request, response);

    }

    public static void updateGoodsInfo(HttpServletRequest request,
                                       HttpServletResponse response) throws ServletException, IOException {

        String cp = Validate.replaceHtml(request.getParameter("cp"));
        String submit = Validate.replaceHtml(request.getParameter("submit"));
        String goodsId = Validate.replaceHtml(request.getParameter("goodsId"));
        String thirdId = Validate.replaceHtml(request.getParameter("thirdId"));
        if (PublicToolCheckParam.checkNullAndEmpty(goodsId)) {
            if (PublicToolCheckParam.checkNullAndEmpty(submit)) {
                if (submit.equals("1")) {
                    String str_goodsName = Validate.replaceHtml(request
                            .getParameter("goodsName"));
                    String typeDetailInfo = Validate.replaceHtml(request
                            .getParameter("typeDetailInfo"));
                    String str_price = Validate.replaceHtml(request
                            .getParameter("price"));
                         String str_photo = Validate.replaceHtml(request
                            .getParameter("photo"));
                    String str_remark = Validate.replaceHtml(request
                            .getParameter("remark"));
                    
                    // String deadline =
                    // Validate.replaceHtml(request.getParameter("deadline");
                     int status = 1;
                    String photo = null;
                    if (PublicToolCheckParam.checkNullAndEmpty(str_goodsName)
                            && PublicToolCheckParam
                            .checkNullAndEmpty(typeDetailInfo)
                            && PublicToolCheckParam
                            .checkNullAndEmpty(str_price)
                            ) {
                        
                        if (PublicToolCheckParam.checkNullAndEmpty(str_photo)) {
                            String[] photoArray = str_photo.split("\\\\");
                            photo = photoArray[photoArray.length - 1];
                        }
                        gdao.updateGoodsInfo(Integer.parseInt(goodsId),
                                typeDetailInfo, str_goodsName,
                                Double.parseDouble(str_price), 1, 1,
                                1, status, photo, str_remark,
                                1, "1");

                        PublicToolShowGoodsInfolist
                                .showGoodsInfoListforthirdId(request, response,
                                        cp, thirdId);
                        request.getRequestDispatcher(
                                "../third/manageGoodsInfo.jsp").forward(
                                request, response);
                    }
                }

            } else {
                GoodsInfo goodsInfo = gdao.getQueryGoodsInfo(Integer
                        .parseInt(goodsId));
                GoodsTypeDAO gtdao = new GoodsTypeDAO();
                ArrayList<Object> typeList = gtdao.getAllType();
                GoodsTypeDetailInfoDAO gtddao = new GoodsTypeDetailInfoDAO();
                ArrayList<Object> typeDetailInfoList = gtddao
                        .GoodsTypeDetailAllInfoDAO(6);
                request.setAttribute("typeList", typeList);
                request.setAttribute("typeDetailInfoList", typeDetailInfoList);
                request.setAttribute("goodsInfo", goodsInfo);
                request.setAttribute("thirdId", thirdId);
                request.getRequestDispatcher("../third/updateGoodsInfo.jsp")
                        .forward(request, response);
            }
        }
    }

    /**
     * Constructor of the object.
     */
    public ManageGoodsInfo() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to
     * post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String method = Validate.replaceHtml(request.getParameter("method"));
        if (PublicToolCheckParam.checkNullAndEmpty(method)) {
            if (method.equals("showAddGoodsInfoPage")) {
                showAddGoodsInfoPage(request, response);
            } else if (method.equals("showManageGoodsInfoPage")) {
                showManageGoodsInfoPage(request, response);
            } else if (method.equals("insert")) {
                insertGoodsInfo(request, response);
            } else if (method.equals("delete")) {
                deleteGoodsInfo(request, response);
            } else if (method.equals("deleteMore")) {
                deleteMoreGoodsInfo(request, response);
            } else if (method.equals("search")) {
                searchGoodsInfo(request, response);
            } else if (method.equals("update")) {
                updateGoodsInfo(request, response);
            } else if (method.equals("getTypeDetailInfo")) {
                getTypeDetailInfo(request, response);
            }

        }
    }

    public static void coolGoodsInfo(HttpServletRequest request,
                                     HttpServletResponse response) throws ServletException, IOException {
        String GoodsId = Validate.replaceHtml(request.getParameter("goodsId"));
        String isCooled = Validate
                .replaceHtml(request.getParameter("isCooled"));
        if (PublicToolCheckParam.checkNullAndEmpty(GoodsId)
                && PublicToolCheckParam.checkNullAndEmpty(isCooled)) {
            gdao.coolGoods(Integer.parseInt(GoodsId),
                    Integer.parseInt(isCooled));
            String isSearching = Validate.replaceHtml(request
                    .getParameter("isSearching"));
            String cp = Validate.replaceHtml(request.getParameter("cp"));
            if (PublicToolCheckParam.checkNullAndEmpty(isSearching)) {
                String key = Validate.replaceHtml(request.getParameter("key"));
                PublicToolShowGoodsInfolist.searchGoodsInfo(request, response,
                        cp, key);

                request.setAttribute("key", key);
                request.setAttribute("isSearching", "yes");
            } else {
                PublicToolShowGoodsInfolist.showGoodsInfoList(request,
                        response, cp);
            }
        }
        request.getRequestDispatcher("../third/manageGoodsInfo.jsp").forward(
                request, response);

    }

    public static void coolMoreGoodsInfo(HttpServletRequest request,
                                         HttpServletResponse response) throws ServletException, IOException {

        String cp = Validate.replaceHtml(request.getParameter("cp"));
        String[] ckb = request.getParameterValues("ckb");
        if (ckb != null) {
            for (int i = 0; i < ckb.length; i++) {
                // cdao.coolGoods(Integer.parseInt(ckb[i]));
            }
        }
        PublicToolShowGoodsInfolist.showGoodsInfoList(request, response, cp);
        request.getRequestDispatcher("../third/manageGoodsInfo.jsp").forward(
                request, response);

    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
